package com.lw.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lw.service.RoleService;
import com.lw.system.Role;
import com.lw.utils.IdWorker;
import com.lw.utils.Result;
import com.lw.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class RoleController extends BaseController{
    @Autowired
    RoleService roleService;
    @Autowired
    IdWorker idWorker;
    //查询全部角色
    @GetMapping(value = "/role")
    public Result findAll(@RequestParam(value = "page") Integer current, @RequestParam(value = "pagesize")Integer size){
        Page<Role> rolePage=roleService.findAll(current,size,companyId);
        return new Result( ResultCode.SUCCESS,rolePage );
    }
    //根据id查询
    @GetMapping("/one")
    public Result findById(@RequestParam(value = "id") String id){
        Role role = roleService.getById( id );
        return  new Result( ResultCode.SUCCESS,role );
    }
    @DeleteMapping("/role")
    public Result delete(@RequestBody Map<String,Object> map){
        roleService.removeById( (String)map.get( "id" ) );
        return Result.SUCCESS();
    }
    @PostMapping("/role")
    public Result save (@RequestBody Role role){
        //设置id 和企业id
        String id=idWorker.nextId()+"";
        role.setId( id );
        role.setCompanyId( companyId );
        roleService.save( role );
        return Result.SUCCESS();
    }
    @PutMapping("/role")
    public Result update(@RequestBody Role role){
        Role temp=roleService.getById( role.getId() );
        temp.setName( role.getName() );
        temp.setDescription( role.getDescription() );
        roleService.updateById( role );
        return Result.SUCCESS();
    }
}
